import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:giraffe/giraffe.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> implements GiraffeCallback {
  final _giraffePlugin = Giraffe();

  int count = 0;
  Map messageReceive = {
    "action":"timeSDK",
    "id":"0.8936220892223257",
    "data":{
      "orderNo":"c675e9d5-a09a-486a-910a-4a3f21f689cb","userId":"1714205177776582656",
      "step":4,"isSubmit":true,
      "appList":false, "sms":false,
      "exif":false,
      "device":false,
      "contact":false,
      "calendar":false,
      "callLog":false,
      "location":false
    },"callback":"webViewToTime"
  };

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(children: [SizedBox()]),
            btn(
              'start',
              () {
                Giraffe().start(this, messageReceive['data']);
              }
            ),
            Column(
              children: [
                Text('${count}'),
                btn('setState', () {
                  setState(() {
                    count++;
                  });
                })
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget btn(String content, Function onPress) {
    return SizedBox(
      width: 150,
      height: 50,
      child: ElevatedButton(
          onPressed: () {
            onPress.call();
          },
          child: Center(
            child: Text(content),
          )
      ),
    );
  }

  @override
  onAfLog(String msg) {
    debugPrint('onAfLog => $msg');
  }

  @override
  onFail(bool isSubmit, int step, String orderNo, Map dataMap) {
    debugPrint('sdk onFail: isSubmit: $isSubmit, step: $step');
  }

  @override
  onPermissionHint() {
    debugPrint('exec permission hint');
  }

  @override
  onSuccess(bool isSubmit, int step, String filePath, String md5, String orderNo, Map dataMap) {
    debugPrint('sdk onSuccess: isSubmit: $isSubmit, step: $step, filePath: $filePath, md5: $md5');
  }
}
